import { createStore } from "vuex";
import {
  setUserCookie,
  removeUserCookie,
  getUserFromCookie,
} from "../utils/cookies.js";
import Cookies from "js-cookie";

export default createStore({
  state: {
    inputTxt: "",
    template: "", // 选择的模板
    basicInfo: [],
    userId: Cookies.get("userId") || "",
    userName: Cookies.get("userName") || "",
    userAllName: getUserFromCookie(),
    token: "",
  },
  getters: {
    getInputTxt(state) {
      return state.inputTxt;
    },
    getTemplate(state) {
      return state.template;
    },
    getBasicInfo(state) {
      return state.basicInfo;
    },
  },
  mutations: {
    setInputTxt(state, inputTxt) {
      state.inputTxt = inputTxt;
    },
    setTemplate(state, template) {
      state.template = template;
    },
    setBasicInfo(state, basicInfo) {
      state.basicInfo = basicInfo;
    },
    updateBasicInfo(state, { field, value }) {
      const item = state.basicInfo.data.find((item) => item.tagCode === field);
      if (item) {
        item.tagValue = value;
      }
    },
    setUser(state, user) {
      state = {
        ...state,
        userId: user.userId,
        userName: user.userName,
        userAll: user,
      };
      setUserCookie(user);
    },
    removeUser() {
      removeUserCookie();
    },
    setToken(state, token) {
      state.token = token;
    },
    removeToken(state) {
      state.token = "";
    },
  },
  actions: {},
  modules: {},
});
